
package com.jf.cloud.user.feign;

import com.jf.cloud.api.order.constant.FinanceType;
import com.jf.cloud.api.payment.vo.ShopAccountDetailVO;
import com.jf.cloud.api.user.dto.FinanceDetailDTO;
import com.jf.cloud.api.user.vo.FinanceDetailExcelVO;
import com.jf.cloud.api.user.vo.FinanceDetailVO;
import com.jf.cloud.api.user.bo.BalancePayBO;
import com.jf.cloud.api.user.bo.BalanceRefundBO;
import com.jf.cloud.api.user.feign.UserBalanceLogClient;
import com.jf.cloud.api.user.vo.UserPayInfoVO;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.util.PageUtil;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.user.constant.RechargeIoTypeEnum;
import com.jf.cloud.user.constant.RechargeTypeEnum;
import com.jf.cloud.user.model.UserBalanceLog;
import com.jf.cloud.user.service.UserBalanceLogService;
import com.jf.cloud.user.service.UserLevelLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.Hidden;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 充值信息
 * @author zz
 */
@RestController
@Hidden
public class UserBalanceLogFeignController implements UserBalanceLogClient {

    @Autowired
    private UserBalanceLogService userBalanceLogService;
    @Autowired
    private UserLevelLogService userLevelLogService;

    @Override
    public ServerResponseEntity<Long> getPayAmount(Long rechargeLogId) {
        UserBalanceLog userBalanceLog = userBalanceLogService.getByBalanceLogId(rechargeLogId);
        return ServerResponseEntity.success(userBalanceLog.getChangeBalance());
    }

    @Override
    public ServerResponseEntity<Integer> getIsPay(Long rechargeLogId) {
        UserBalanceLog userBalanceLog = userBalanceLogService.getByBalanceLogId(rechargeLogId);
        return ServerResponseEntity.success(userBalanceLog.getIsPayed());
    }

    @Override
    public ServerResponseEntity<Void> insertBalancePayLog(BalancePayBO balancePayBO) {
        Long userId = AuthUserContext.get().getUserId();
        UserBalanceLog userRechargeLog = new UserBalanceLog();
        userRechargeLog.setUserId(userId);
        userRechargeLog.setChangeBalance(balancePayBO.getChangeBalance());
        userRechargeLog.setIoType(RechargeIoTypeEnum.EXPENDITURE.value());
        if (Objects.nonNull(balancePayBO.getIsVip()) && Objects.equals(balancePayBO.getIsVip(),true)){
            userRechargeLog.setType(RechargeTypeEnum.VIP.value());
        } else {
            userRechargeLog.setType(RechargeTypeEnum.PAY.value());
        }
        userRechargeLog.setIsPayed(0);
        userRechargeLog.setPayId(balancePayBO.getPayId());
        userBalanceLogService.save(userRechargeLog);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<Void> doRefund(BalanceRefundBO balanceRefundBO) {
        userBalanceLogService.doRefund(balanceRefundBO);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<PageVO<FinanceDetailVO>> pageFinanceDetail(FinanceDetailDTO financeDetailDTO) {
        PageDTO pageDTO = new PageDTO();
        pageDTO.setPageSize(financeDetailDTO.getPageSize());
        pageDTO.setPageNum(financeDetailDTO.getPageNum());
        if(Objects.equals(financeDetailDTO.getFinanceBizType(), 5)){
            PageVO<FinanceDetailVO> result = userBalanceLogService.pageFinanceDetailByQrCode(pageDTO, financeDetailDTO);
            return ServerResponseEntity.success(result);
        }else{
            PageVO<FinanceDetailVO> result = userBalanceLogService.pageFinanceDetail(pageDTO, financeDetailDTO);
            return ServerResponseEntity.success(result);
        }

    }



    @Override
    public ServerResponseEntity<List<FinanceDetailExcelVO>> listFinanceDetail(FinanceDetailDTO financeDetailDTO) {
        List<FinanceDetailExcelVO> listFinanceDetail = userBalanceLogService.listFinanceDetail(financeDetailDTO);
        return ServerResponseEntity.success(listFinanceDetail);
    }


    @Override
    public ServerResponseEntity<ShopAccountDetailVO> getPlatformAccountDetail(Date startTime, Date endTime) {
        ShopAccountDetailVO shopAccountDetailVO = userBalanceLogService.getPlatformAccountDetail(startTime, endTime);
        return ServerResponseEntity.success(shopAccountDetailVO);
    }

    @Override
    public ServerResponseEntity<PageVO<UserPayInfoVO>> pageUserPayInfo(Date startTime, Date endTime, Integer financeType, Integer pageSize, Integer pageNum) {
        PageDTO pageDTO = new PageDTO();
        pageDTO.setPageSize(pageSize);
        pageDTO.setPageNum(pageNum);
        PageVO<UserPayInfoVO> pageVO = new PageVO<>();
        if (Objects.equals(financeType, FinanceType.RECHARGE.value())) {
            pageVO =  PageUtil.doPage(pageDTO, () -> userBalanceLogService.listPayInfoVO(startTime, endTime));
        } else if (Objects.equals(financeType, FinanceType.LEVEL.value())) {
            pageVO =  PageUtil.doPage(pageDTO, () -> userLevelLogService.listPayInfoVO(startTime, endTime));
        }
        return ServerResponseEntity.success(pageVO);
    }

    @Override
    public ServerResponseEntity<List<UserPayInfoVO>> listUserPayInfo(Date startTime, Date endTime, Integer financeType) {
        List<UserPayInfoVO> result = new ArrayList<>();
        if (Objects.equals(financeType, FinanceType.RECHARGE.value())) {
            result = userBalanceLogService.listPayInfoVO(startTime, endTime);
        } else if (Objects.equals(financeType, FinanceType.LEVEL.value())) {
            result = userLevelLogService.listPayInfoVO(startTime, endTime);
        }
        return ServerResponseEntity.success(result);
    }

}
